package com.bs.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.bs.bean.ResultInfo;
import com.bs.bean.User;
import com.bs.service.AdminService;
import com.bs.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class AdminController {

    @Autowired
    private AdminService adminService;

    //管理员登录
    @PostMapping(value = "/adminLogin")
    public String adminLogin(String username, String password, HttpServletRequest request) {

        password = MD5Utils.md5(password);
        User admin = adminService.adminLogin(username,password);

        if(admin!=null) {
            request.getSession().setAttribute("admin",admin);

            //登录成功
            return "admin/adminHome";
        }
//        model.addAttribute("","");
        return "redirect:/admin";
    }

    @GetMapping("/adminIsLogin")
    @ResponseBody
    public ResultInfo checkJsessionid(HttpSession session) {

        User admin = (User) session.getAttribute("admin");

        //检验是否存在user值来判断是否以及登录
        if (admin != null) {

            //将已登录的用户的头像和用户名返回
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("userPicture", admin.getPicture());
            resultMap.put("username", admin.getUsername());
            return ResultInfo.successInfo("已登录！", resultMap);

        } else {
            return ResultInfo.failInfo("请先登录！");
        }

    }

    //用户的模糊查询
    @GetMapping(value = "/selectByName")
    public String selectByName(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                               ModelMap model,String username) {
        PageHelper.startPage(pageNum,5);
        List<User> list = adminService.selectByName(username);

        PageInfo pageInfo = new PageInfo(list);

        model.addAttribute("userList",pageInfo);
        return "admin/user/allUser";
    }

    //根据id删除用户
    @GetMapping(value = "/deleteUserById")
    public String deleteUserById(Integer id) {

        boolean result = adminService.deleteUserById(id);

        if(result) {
            return "forward:/getAllUser";
        }

        return "failed";
    }

    @GetMapping(value = "/changeStateById")
    public String changeStateById(Integer id) {

        User user = adminService.getUserById(id);
        if(user.getState()==1){
            user.setState(2);
        }else{
            user.setState(1);
        }
        boolean result = adminService.updateStateByUser(user);

        if(result) {
            return "forward:/getAllUser";
        }

        return "failed";
    }

    @GetMapping(value = "/changeRoleById")
    public String changeRoleById(Integer id) {
        User user = adminService.getUserById(id);

        if(user.getRoleId()==2){
            user.setRoleId(3);
            user.setIsUpload(1);
        }else{
            user.setRoleId(2);
            user.setIsUpload(0);
        }
        boolean result = adminService.changeRoleByUser(user);
        if(result) {
            return "forward:/getAllUser";
        }

        return "failed";
    }


    //查询所有用户
    @RequestMapping(value = "/getAllUser")
    public String getAllUser(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                             ModelMap model) {

        PageHelper.startPage(pageNum,5);
        List<User> list = adminService.getAllUser();
        PageHelper.orderBy("roleId desc");
        PageInfo pageInfo = new PageInfo(list);

        model.addAttribute("userList",pageInfo);

        return "admin/user/allUser";
    }


    //管理员退出登录
    @GetMapping(value = "/adminLogout")
    public String adminLogout(HttpServletRequest request) {

        request.getSession().invalidate();
            return "redirect:/getAllNews";
    }

    //管理员修改密码
    @RequestMapping(value = "/updateAdminPassword")
    public String updateAdminPassword(User admin,ModelMap model){
        String password = MD5Utils.md5(admin.getPassword());
        admin.setPassword(password);
        boolean result = adminService.updateAdminPassword(admin);

        if(result) {
            model.addAttribute("msg","修改密码成功，请重新登录");
            return "redirect:/admin";
        }
        return "failed";
    }


}
